﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Grishko.DataTier;
using System.Data;
using OutdoorLib.BaseEntity.Face;

public partial class Ctl_ctlClientList_ : System.Web.UI.UserControl
{
    
    const string IsListModeKey = "IsListMode";
    const string SelectedIDKey = "SelectedID";
    const string SelectedTextKey = "SelectedText";
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            IsListMode = true ;
    }

    public override void DataBind()
    {
        mvModeFrame.ActiveViewIndex = (IsListMode ? 1 : 0);
        if (IsListMode)
        {
            lstCliectList.Items.Clear();
            lstCliectList.Items.AddRange(GetList().ToArray<ListItem>());
            base.DataBind();
        }
    }
    
    public bool IsListMode 
    {

        get
        {
            object text = ViewState[IsListModeKey];
            if (null == text)
                return false;
            else
                return (bool)text;
        }
        set
        {
            ViewState[IsListModeKey] = value;
        }
    }

    public string SelectedText
    {

        get
        {
            object text = ViewState[SelectedTextKey];
            if (null == text)
                return string.Empty ;
            else
                return (string )text;
        }
        set
        {
            ViewState[SelectedTextKey] = value;
        }
    }

    public string SelectedID
    {

        get
        {
            object text = ViewState[SelectedIDKey];
            if (null == text)
                return string.Empty;
            else
                return (string)text;
        }
        set
        {
            ViewState[SelectedIDKey] = value;
        }
    }

    public Client CurrentClient
    {
        get 
        { 
            if(String.IsNullOrEmpty ( SelectedID ))
                return null ;

            return (Client )AppEnvironment.Helper.GetObject(SelectedID);
        }
    }

    #region Source
    List<ListItem> GetList()
    {
        string SqlFormat="select ObjectID,PropertyValue  from VSrchObjectPropertyStorage where TagName ='Client' and PropertyName='Name' and PropertyValue like'%{0}%'";
        List<ListItem> result = new List<ListItem>();
        if (txtSearchValue.Text.Length < 3)
            return result;

        SqlTools tls = new SqlTools(AppContext.GetSC ());
        DataSet DS=tls.ExecuteSql(string .Format (SqlFormat ,txtSearchValue.Text));
        if (DS.Tables.Count > 0)
            foreach (DataRow rw in DS.Tables[0].Rows)
            {
                ListItem i = new ListItem() { Value = rw[0].ToString(), Text = rw[1].ToString() };
                result.Add (i);

            }

        return result;

    }
    #endregion
    
    /// <summary>
    /// Добавить клиента в БД
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveClient_Click(object sender, ImageClickEventArgs e)
    {
        Client  _Client = (Client)AppEnvironment.ObjectFactory.GetNewObject("Client", GlobalConstants.GetNewID(Guid .NewGuid ().ToString () ));        
        AppEnvironment.BusinessRules.SetDefaultValue(_Client,null);
        _Client.Name = txtSearchValue.Text;
        AppEnvironment.ObjectFactory.SaveObject(_Client);

        IsListMode = (!IsListMode);
        SelectedID = _Client.ID;
        txtClientName.Text = _Client.FullName ;
        DataBind();


    }
    protected void txtFind_KeyDownEnter(object sender, EventArgs e)
    {
        DataBind();
    }
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        DataBind();

    }

    protected void lstCliectList_SelectedIndexChanged(object sender, EventArgs e)
    {
        IsListMode = false ;
        SelectedID = lstCliectList.SelectedValue;
        txtClientName.Text = lstCliectList.SelectedItem .Text ;
        DataBind();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        IsListMode = true;
        DataBind();
    }
}
